unit SmallStruct.ActionView.TSSActionView;

interface

uses
  Classes,
  SmallStruct.Core,
  SmallStruct.Form.TSSFrame,
  SmallStruct.ActionView,
  SmallStruct.View,
  SmallStruct.Configuration,
  Controls,
  StdCtrls,
  SmallStruct.Core.TSSObjectContext,
  SmallStruct.Controller;

type
  TSSActionViewContext = class(TSSObjectContext, ISSActionViewContext)
  private
    _RequestActionViewName: string;
    _Controller: ISSController;
    _SystemObject: ISSSystemObject;
  public
    function GetRequestActionViewName: string;
    function GetController: ISSController;
  public
    procedure SetRequestActionViewName(const ARequestActionViewName: string);
    procedure SetController(AController: ISSController);
    function GetSystemObject: ISSSystemObject;
    procedure SetSystemObject(ASystemObject: ISSSystemObject);
  end;

  TSSActionView = class(TSSFrame, ISSActionView)
    procedure FrameResize(Sender: TObject);
  private
    _View: ISSView;
    _ActionPanel: ISSActionPanel;
  protected
    procedure _ActionButtonClick(Sender: TObject);
  protected
    procedure InitActionPanel;
  protected
    function GetView: ISSView;
    procedure CloseActionView; virtual;
    procedure DisableActionView; virtual;
    procedure DisplayActionView; virtual;
    procedure EnableActionView; virtual;
    procedure SetView(AView: ISSView); virtual;
  public
    procedure InitObject; override;
  public
    destructor Destroy; override;
  end;

  TSSActionViewClass = class of TSSActionView;

implementation

uses
  RzCommon,
  RzPanel,
  Smallstruct.View.TSSDataGridView,
  SmallStruct.ActionView.TSSActionPanel;

{$R *.dfm}

{ TSSActionView }

procedure TSSActionView.CloseActionView;
begin
  Visible := False;
end;

destructor TSSActionView.Destroy;
begin
  _ActionPanel.ReleaseObjectInstance;
  inherited;
end;

procedure TSSActionView.DisableActionView;
begin
  Enabled := False;
end;

procedure TSSActionView.DisplayActionView;
begin
  Visible := True;
end;

procedure TSSActionView.EnableActionView;
begin
  Enabled := True;
end;

procedure TSSActionView.FrameResize(Sender: TObject);
begin
  inherited;

  if _View.GetObjectInstance is TSSDataGridView then
    (_View.GetObjectInstance as TWinControl).Align := alClient
  else
  begin
    (_View.GetObjectInstance as TWinControl).Align := alNone;
    (_View.GetObjectInstance as TWinControl).Top := (ClientHeight - (_View.GetObjectInstance as TWinControl).Height) div 2 - (_ActionPanel.GetObjectInstance as TWinControl).Height div 2;
    (_View.GetObjectInstance as TWinControl).Left := (ClientWidth - (_View.GetObjectInstance as TWinControl).Width) div 2;
  end;
end;

function TSSActionView.GetView: ISSView;
begin
  Result := _View;
end;

procedure TSSActionView.InitActionPanel;
var
  ObjectInstance: TComponent;
  ActionPanelContext: TSSActionPanelContext;
  Index: Integer;
  ActionConfiguration: ISSObjectContainer;
begin
  ObjectInstance := TComponentClass(((GetContext as ISSActionViewContext).GetSystemObject.GetClassTable as ISSClassTable).QueryClass(SS_ACTION_PANEL_NAME)).Create(Self);
  _ActionPanel := ObjectInstance as ISSActionPanel;
  ActionPanelContext := TSSActionPanelContext.Create(Self);

  ActionConfiguration := (GetContext.GetConfiguration as ISSActionViewConfiguration).GetActionConfiguration;

  ActionPanelContext.SetActionConfiguration(ActionConfiguration);
  _ActionPanel.SetContext(ActionPanelContext);
  _ActionPanel.InitObject;

  for Index := 0 to _ActionPanel.GetActionButtonContainer.GetCount - 1 do
    (_ActionPanel.GetActionButtonContainer.GetObject(Index) as ISSActionButton).SetClickEvent(_ActionButtonClick);

  (_ActionPanel.GetObjectInstance as TWinControl).Parent := Self;
  (_ActionPanel.GetObjectInstance as TWinControl).Align := alBottom;
end;

procedure TSSActionView.InitObject;
var
  Panel: TRzPanel;
begin
  InitActionPanel;
  Panel := TRzPanel.Create(Self);
  Panel.Parent := Self;
  Panel.Align := alBottom;
  Panel.BorderOuter := fsNone;
  Panel.Height := 4;
  Panel.Transparent := True;
end;

procedure TSSActionView.SetView(AView: ISSView);
var
  Panel: TRzPanel;
begin
  _View := AView;
  (_View.GetObjectInstance as TWinControl).Parent := Self;
end;

procedure TSSActionView._ActionButtonClick(Sender: TObject);
begin
  (GetContext as ISSActionViewContext).GetController.RequestAction(((GetContext as ISSActionViewContext).GetConfiguration as ISSActionViewConfiguration).GetActionViewName, ((Sender as TComponent).Owner as ISSActionButton).GetActionCaption);
end;

{ TSSActionViewContext }

function TSSActionViewContext.GetController: ISSController;
begin
  Result := _Controller;
end;

function TSSActionViewContext.GetRequestActionViewName: string;
begin
  Result := _RequestActionViewName;
end;

function TSSActionViewContext.GetSystemObject: ISSSystemObject;
begin
  Result := _SystemObject;
end;

procedure TSSActionViewContext.SetController(AController: ISSController);
begin
  _Controller := AController;
end;

procedure TSSActionViewContext.SetRequestActionViewName(const ARequestActionViewName: string);
begin
  _RequestActionViewName := ARequestActionViewName;
end;

procedure TSSActionViewContext.SetSystemObject(ASystemObject: ISSSystemObject);
begin
  _SystemObject := ASystemObject;
end;

end.
